/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Classes;
import vn.webapp.model.Users;
import vn.webapp.model.User;

@Repository("userDAO")
@SuppressWarnings({"unchecked", "rawtypes"})

public class UserDAOImpl extends BaseDao implements UserDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /**
     *
     * @param username
     * @return
     */
    @Override
    public Users getByUsername(String username) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Users.class);
            criteria.add(Restrictions.eq("username", username));
            Users user = (Users) criteria.uniqueResult();
            commit();
            return user;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }

    /**
     *
     * @return
     */
    @Override
    public List<Users> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Users.class);
            criteria.setMaxResults(10);
            criteria.setFirstResult(0);
            List<Users> users = criteria.list();
            commit();
            return users;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }

    @Override
    public Users viewDetail(String id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "from Users u where u.id = '" + id + "'");
        return (Users) q.uniqueResult();
    }

    @Override
    public int removeUser(String id) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "delete from Users u where u.id ='" + id + "'");
        int result = query.executeUpdate();
        return result;
    }

    @Override
    public int saveAUser(Users user) 
    {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(user);
           commit();
           return id;           
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Users> filter(int page, int pageSize) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Users.class);
            criteria.setMaxResults(pageSize);
            if (page > 0) {
                criteria.setFirstResult(page * pageSize);
            } else {
                criteria.setFirstResult(page);
            }
            List<Users> users = criteria.list();
            commit();
            return users;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public int count(){
        //int size = 0;
         try {
            begin();
            Criteria criteria = getSession().createCriteria(Users.class);            
            List<Users> users = criteria.list();
            commit();
            return users.size();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
}
